
package mock.dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import mock.dto.LogDTO;
import mock.utility.Utility;

public class LogDAOImp implements LogDAO {

	private Utility util = new Utility();
	
	/**
	 * input: cardNo, date
	 * output: List LogDTO
	 */
	@Override
	public List<LogDTO> getLogByCardNoAndDate(String cardNo, Date date) {
		// TODO Auto-generated method stub
		Connection con = util.createConnection();
		if(con != null){
			PreparedStatement ps = null;
			ResultSet rs = null;
			try {
				ps = con.prepareStatement("select * from Log where CardNo = ? and LogDate = ?");
				ps.setString(1, cardNo);
				ps.setDate(2, date);
				rs = ps.executeQuery();
				List<LogDTO> list = new ArrayList<>();
				while(rs.next()){
					int logID = rs.getInt("LogID");
					int logTypeID= rs.getInt("LogTypeID");
					int atmID= rs.getInt("ATMID");
					String cardNO = rs.getString("CardNo");
					Date logDate = rs.getDate("LogDate");
					double amount = rs.getDouble("Amount");
					String detail = rs.getString("Details");
					LogDTO log = new LogDTO(logID, logTypeID, atmID, cardNO, logDate, amount, detail);
					list.add(log);
				}
				if(list.isEmpty()) return null;
				return list;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
			}finally{
				if(con != null)
					try {
						con.close();
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				if(ps != null)
					try {
						ps.close();
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				if(rs != null)
					try {
						rs.close();
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
			}
		}else{
			return null;
		}
	}
	
	/**
	 * input: cardNo, date, logTypeID
	 * output: List LogDTO
	 */
	@Override
	public List<LogDTO> getLogByCardNoAndDateAndLogTypeID(String cardNo,
			Date date, int logTypeID) {
		// TODO Auto-generated method stub
		Connection con = util.createConnection();
		if(con != null){
			PreparedStatement ps = null;
			ResultSet rs = null;
			try {
				ps = con.prepareStatement("select * from Log where CardNo = ? and LogDate = ? and LogTypeID = ?");
				ps.setString(1, cardNo);
				ps.setDate(2, date);
				ps.setInt(3, logTypeID);
				rs = ps.executeQuery();
				List<LogDTO> list = new ArrayList<>();
				while(rs.next()){
					int logID = rs.getInt("LogID");
					int logTypeid= rs.getInt("LogTypeID");
					int atmID= rs.getInt("ATMID");
					String cardNO = rs.getString("CardNo");
					Date logDate = rs.getDate("LogDate");
					double amount = rs.getDouble("Amount");
					String detail = rs.getString("Details");
					LogDTO log = new LogDTO(logID, logTypeid, atmID, cardNO, logDate, amount, detail);
					list.add(log);
				}
				if(list.isEmpty()) return null;
				return list;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
			}finally{
				if(con != null)
					try {
						con.close();
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				if(ps != null)
					try {
						ps.close();
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				if(rs != null)
					try {
						rs.close();
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
			}
		}else{
			return null;
		}
	}
	
	
}
